package com.situ.ssm.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.situ.ssm.entity.PageInfo;
import com.situ.ssm.entity.Student;
import com.situ.ssm.mapper.StudentMapper;
import com.situ.ssm.service.IStudentService;
import com.situ.ssm.util.MyBatisUtil;

// 实现类的命名以Impl作为结尾
public class StudentServiceImpl implements IStudentService{
	// private IStudentDao studentDao = new StudentDaoImpl();
	private StudentMapper studentMapper;
	
	public StudentServiceImpl() {
		SqlSession sqlSession = MyBatisUtil.getSqlSession();
		studentMapper = sqlSession.getMapper(StudentMapper.class);
	}

	@Override
	public PageInfo findByPage(Integer pageNo, Integer pageSize) {
		PageInfo pageInfo = new PageInfo();
		pageInfo.setPageNo(pageNo);
		pageInfo.setPageSize(pageSize);
		
		// totalPage 
		int count = studentMapper.getTotalCount();
		int totalPage = (int)Math.ceil((double)count / pageSize);
		pageInfo.setTotalPage(totalPage);
		
		// list
		// pageNo,pageSize-->limit (pageNo-1)*pageSize,pageSize
		int offset = (pageNo - 1) * pageSize;
		List<Student> list = studentMapper.findByPage(offset, pageSize);
		pageInfo.setList(list);
		
		return pageInfo;
	}

	@Override
	public boolean deleteById(Integer id) {
		int count = studentMapper.deleteById(id);
		return count == 1 ? true : false;
	}

}
